package com.zhuoli.service.springboot.local.current.limit.controller;

import com.zhuoli.service.springboot.local.current.limit.annotation.Limit;
import com.zhuoli.service.springboot.local.current.limit.service.UserControllerService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zhuoli
 * @Date: 2018/9/6 13:37
 * @Description:
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private UserControllerService userControllerService;

    @Limit(prefix = "get_user", key = "test", count = 10, expireTime = 2)
    @RequestMapping(value = "/get_user", method = RequestMethod.POST)
    public ResponseEntity getUserById(@RequestParam Long id){
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getUserById(id));
    }

    /*用于测试不同的key，限流是否生效*/
    @Limit(prefix = "get_user1", key = "test", count = 5, expireTime = 1)
    @RequestMapping(value = "/get_user1", method = RequestMethod.POST)
    public ResponseEntity getUserById1(@RequestParam Long id){
        return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getUserById(id));
    }
}
